package com.tjec.project.portal.controller;

import com.tjec.common.enums.NumCode;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.portal.domain.BmsMsg;
import com.tjec.project.portal.service.IBmsMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息中心Controller
 *
 * @author liuxu
 * @date 2020-05-07
 */
@Api(value = "消息中心Controller", tags = {"消息中心接口"})
@RestController
@RequestMapping("/portal/bmsMsg")
public class BmsMsgController extends BaseController {
    @Autowired
    private IBmsMsgService bmsMsgService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询系统消息列表
     */
    @ApiOperation("获取消息列表")
    @ApiImplicitParam(name = "content", value = "消息内容", required = false, dataType = "String", paramType = "query")
    @GetMapping("/list")
    public TableDataInfo list(BmsMsg bmsMsg) {
        startPage();
        String guid = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        bmsMsg.setReceiverUserId(guid);
        List<BmsMsg> list = bmsMsgService.selectBmsMsgList(bmsMsg);
        return getDataTable(list);
    }

    /**
     * 查询系统消息数量
     */
    @ApiOperation("获取消息数量")
    @GetMapping("/count")
    public AjaxResult list() {
        Map<String, Integer> map = new HashMap<>();
        Integer count = 0;
        BmsMsg bmsMsg = new BmsMsg();

        String guid = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        bmsMsg.setReceiverUserId(guid);
        bmsMsg.setStatus(NumCode.ZERO.getCode());
        List<BmsMsg> list = bmsMsgService.selectBmsMsgList(bmsMsg);
        if (!CollectionUtils.isEmpty(list)) {
            count = list.size();
        }
        map.put("count", count);
        return AjaxResult.success(map);
    }

    /**
     * 查询系统消息数量
     */
    @ApiOperation("一键已读")
    @GetMapping("/batchRead")
    public AjaxResult batchRead() {
        BmsMsg bmsMsg = new BmsMsg();

        String guid = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        bmsMsg.setReceiverUserId(guid);
        bmsMsg.setStatus(NumCode.ONE.getCode());
        int ret = bmsMsgService.batchRead(bmsMsg);
        if (ret > -1) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

    /**
     * 导出系统消息列表
     */
    @PreAuthorize("@ss.hasPermi('portal:bmsMsg:export')")
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsMsg bmsMsg) {
        List<BmsMsg> list = bmsMsgService.selectBmsMsgList(bmsMsg);
        ExcelUtil<BmsMsg> util = new ExcelUtil<BmsMsg>(BmsMsg.class);
        return util.exportExcel(list, "bmsMsg");
    }

    /**
     * 获取系统消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('portal:bmsMsg:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid) {
        return AjaxResult.success(bmsMsgService.selectBmsMsgByGuid(guid));
    }

    /**
     * 新增系统消息
     */
    @ApiOperation("新增消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "msgType", value = "消息类型", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "sendUserId", value = "发送者", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "sendUserName", value = "发送者名称", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "receiverUserId", value = "接收人", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "receiverUserName", value = "接收人名称", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "content", value = "消息内容", required = true, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "sendTime", value = "发送时间", required = true, dataType = "Date", paramType = "form")
    })
    @PreAuthorize("@ss.hasPermi('portal:bmsMsg:add')")
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsMsg bmsMsg) {
        return toAjax(bmsMsgService.insertBmsMsg(bmsMsg));
    }

    /**
     * 修改系统消息
     */
    @PreAuthorize("@ss.hasPermi('portal:bmsMsg:edit')")
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsMsg bmsMsg) {
        return toAjax(bmsMsgService.updateBmsMsg(bmsMsg));
    }

    /**
     * 删除系统消息
     */
    @PreAuthorize("@ss.hasPermi('portal:bmsMsg:remove')")
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(bmsMsgService.deleteBmsMsgByGuids(guids));
    }
}
